package wsf.akkascala

import java.util
import java.util.StringTokenizer

import akka.actor.{Actor, ActorRef}
import akkascala.{MapData, Word}

/**
 * Created by sjzx on 2016/5/13.
  * Map Actor
  * receive string then map to List[Word] send to reduce actor
 */
class MapWorker(reduceActor : ActorRef) extends Actor {
  override def receive={
    case s:String => reduceActor ! doMap(s)//receive string
    case _ => unhandled()
  }

  def doMap(str:String) : MapData={
//    MapData(str.split(" ").toList.map(s => Word(s.toLowerCase, 1)))
    //use StringTokenizer
    var dataList = List[Word]()
    var parser = new StringTokenizer(str)
    while(parser.hasMoreTokens){
      val word = parser.nextToken().toLowerCase
      dataList = new Word(word, 1) :: dataList
    }
    MapData(dataList)
  }
}

